package com.book.controller;

import com.book.common.BaseController;
import com.book.pojo.Category;
import com.book.remote.BookApiRemote;
import com.book.remote.CategoryRemoter;
import com.book.service.UserService;
import com.book.util.CodeUtil;
import com.book.util.Constants;
import com.book.vo.BookIndexVo;
import com.book.vo.BookNewVo;
import com.book.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author 李锦程
 * @Date 2021/4/2 3:36 下午
 * @Package com.book.controller
 * @Version 1.0
 */
@Slf4j
@Controller
public class IndexController extends BaseController {
    @Autowired
    private BookApiRemote uBookApi;
    @Autowired
    private CategoryRemoter categoryRemoter;
    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String index(Model model) {
        List<BookIndexVo> uBookList = uBookApi.getUBookList();
        model.addAttribute("uBookList", uBookList);
        List<Category> categoryList = categoryRemoter.getCategoryList(0);
        model.addAttribute("cateList",categoryList);
        List<BookNewVo> bookThree = uBookApi.getBookThree();
        model.addAttribute("newBooks",bookThree);
        return "index2.jsp";
    }

    @RequestMapping("/index")
    public String Index(Model model, HttpSession session) {
        List<BookIndexVo> uBookList = uBookApi.getUBookList();
        model.addAttribute("uBookList", uBookList);
        return "index.jsp";
    }

    @RequestMapping("/login/index")
    public String login() {
        return "login.jsp";
    }

    @RequestMapping("/register/index")
    public String register() {
        return "register.jsp";
    }

    @RequestMapping(value = "/getVerify")
    public void getVerify(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("image/jpeg");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        try {
            CodeUtil.getRandCode(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/checkCode")
    @ResponseBody
    public String checkCode(HttpSession session, String code) {
        code=code.toUpperCase();
        String sessionCode = (String) session.getAttribute(CodeUtil.RANDOM_CODE_KEY);
        if (sessionCode.equals(code)) {
            return Constants.SUCCESS;
        }
        return Constants.ERROR;
    }

    @RequestMapping("/getCategory")
    @ResponseBody
    public ResponseVo getCategory(Integer parentId){
        List<Category> categoryList = categoryRemoter.getCategoryList(parentId);
        return ResponseVo.successResult(categoryList);
    }

}

